package com.quan.hr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quan.commons.core.MyBaseServiceImpl;
import com.quan.commons.utils.DateUtils;
import com.quan.commons.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.quan.hr.commons.vo.HrPostCoefficientVo;
import com.quan.hr.entity.HrPostCoefficient;
import com.quan.hr.mapper.HrPostCoefficientMapper;
import com.quan.hr.service.HrPostCoefficientService;


@Slf4j
@Service
public class HrPostCoefficientServiceImpl extends MyBaseServiceImpl<HrPostCoefficientMapper, HrPostCoefficient> implements HrPostCoefficientService {

    @Autowired
    private HrPostCoefficientMapper mapper;

    @Override
    public BigDecimal getPostCoefficient(Long id, String year) {
        final LambdaQueryWrapper<HrPostCoefficient> queryWrapper = new QueryWrapper<HrPostCoefficient>().lambda();
        queryWrapper.eq(HrPostCoefficient::getPostId, id);
        if(StringUtils.isNotBlank(year)) {
            queryWrapper.eq(HrPostCoefficient::getYear, year);
        } else {
            queryWrapper.eq(HrPostCoefficient::getYear, DateUtils.getYear());
        }
        final HrPostCoefficient coefficient = super.getOne(queryWrapper);
        if(null != coefficient) {
            return coefficient.getCoeff();
        }
        return null;
    }

    @Override
    public List<HrPostCoefficientVo> queryList(PageUtils pageUtils, HrPostCoefficientVo vo) {
        QueryWrapper<HrPostCoefficient> queryWrapper = new QueryWrapper<HrPostCoefficient>(vo);

        super.handleSort(queryWrapper, pageUtils);

        List<HrPostCoefficient> list = super.list(pageUtils, queryWrapper);
        List<HrPostCoefficientVo> collect = list.stream().map(item -> {
                HrPostCoefficientVo bean = new HrPostCoefficientVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public PageUtils queryPage(PageUtils pageUtils, HrPostCoefficientVo vo) {
        QueryWrapper<HrPostCoefficient> queryWrapper = new QueryWrapper<HrPostCoefficient>(vo);

        if (StringUtils.isNotBlank(vo.getKeyword())) {
            queryWrapper.and(w -> {
                w.like("name", vo.getKeyword());
            });
        }

        final IPage<HrPostCoefficient> page = super.selectPage(pageUtils, queryWrapper);

        PageUtils ps = new PageUtils(page);

        final List<HrPostCoefficient> records = page.getRecords();

        final List<HrPostCoefficient> collect = records.stream().map((item) -> {
                HrPostCoefficientVo bean = new HrPostCoefficientVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        ps.setRecords(collect);
        return ps;
    }
}